# NAME 库名
# VERSION 库版本
# SOURCES 库源码 cpp
# INCLUDES 库头文件
function(build_library)
    # 定义要解析的参数类型
    set(options       )                    # 布尔选项（如 VERBOSE, STATIC）
    set(oneValueArgs  NAME)                # 单值参数
    set(multiValueArgs VERSION SOURCES INCLUDES)  # 多值参数
    # 解析参数
    cmake_parse_arguments(ARGV "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
   file(GLOB_RECURSE SRC ${ARGV_SOURCES}/*.cpp ${ARGV_INCLUDES}/*.h)
    # 创建动态库库 library_instal_config
    if(BUILD_SHARED_LIBS)
    add_library(${ARGV_NAME}-shared SHARED ${SRC})
    #连接头文件
    target_include_directories(${ARGV_NAME}-shared PRIVATE ${ARGV_INCLUDES})
    set_target_properties(${ARGV_NAME}-shared PROPERTIES
        VERSION ${ARGV_VERSION}
        SOVERSION 1
        OUTPUT_NAME ${ARGV_NAME}
        NAMESPACE ${ARGV_NAME}::shared
        INTERFACE_INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${ARGV_INCLUDES}>;$<INSTALL_INTERFACE:include>"
    )
     # 动态库也必须用 -fPIC 编译，才能被静态库链接
    if(POSITION_INDEPENDENT_CODE)
        set_property(TARGET "${ARGV_NAME}-shared" PROPERTY POSITION_INDEPENDENT_CODE ON)
    endif()
    add_library(${ARGV_NAME}::${ARGV_NAME}-shared ALIAS ${ARGV_NAME}-shared)
    # 安装库文件
        install(TARGETS ${ARGV_NAME}-shared
            EXPORT ${ARGV_NAME}-shared-Targets
            RUNTIME DESTINATION bin
            LIBRARY DESTINATION lib
            ARCHIVE DESTINATION lib
            INCLUDES DESTINATION include
        )
    # 安装头文件
    if(BUILD_SHARED_LIBS)
        install(DIRECTORY ${ARGV_INCLUDES}/ DESTINATION include)
    endif()
    # 生成并安装 MyLibTargets.cmake 等文件
    install(EXPORT ${ARGV_NAME}-shared-Targets
        DESTINATION lib/cmake/${ARGV_NAME}
        NAMESPACE ${ARGV_NAME}::
    )
    endif()

    # 创建静态库
    if(BUILD_STATIC_LIBS)
        add_library(${ARGV_NAME}-static STATIC ${SRC})
        #连接头文件
        target_include_directories(${ARGV_NAME}-static PRIVATE ${ARGV_INCLUDES})
        set_target_properties(${ARGV_NAME}-static PROPERTIES
            OUTPUT_NAME ${ARGV_NAME}     # 输出库名
            INTERFACE_INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${ARGV_INCLUDES}>;$<INSTALL_INTERFACE:include>"
        )
        if(POSITION_INDEPENDENT_CODE)
            # 静态库也必须用 -fPIC 编译，才能被共享库链接
            set_property(TARGET ${ARGV_NAME}-static PROPERTY POSITION_INDEPENDENT_CODE ON)
        endif()
        add_library(${ARGV_NAME}::${ARGV_NAME}-static ALIAS ${ARGV_NAME}-static)
        # 安装静态库
        install(TARGETS ${ARGV_NAME}-static
            EXPORT ${ARGV_NAME}-static-Targets
            ARCHIVE DESTINATION lib
            INCLUDES DESTINATION include
        )
        install(EXPORT ${ARGV_NAME}-static-Targets
            DESTINATION lib/cmake/${ARGV_NAME}
            NAMESPACE ${ARGV_NAME}::
        )
    endif()
    # 生成动态库的 cmake 文件
    set(IN_CONFIG_PATH "${CMAKE_CURRENT_BINARY_DIR}/cmake/${ARGV_NAME}-config.cmake.in" CACHE PATH "生成动态库的 cmake 文件")
        # 生成动态库的 cmake 文件
        file(WRITE "${IN_CONFIG_PATH}"
        "@PACKAGE_INIT@ \n"
        "get_filename_component(${ARGV_NAME}_CMAKE_DIR \"\${CMAKE_CURRENT_LIST_FILE}\" PATH) \n"
        "file(GLOB CONFIG_FILES \"\${${ARGV_NAME}_CMAKE_DIR\}/${ARGV_NAME}-*-Targets.cmake\") \n"
        "foreach(f $\{CONFIG_FILES\}) \n"
          "include($\{f\}) \n"
        "endforeach() \n"
        "include(\"\${CMAKE_CURRENT_LIST_DIR}/${ARGV_NAME}ConfigVersion.cmake\") \n"
        "# 检查 required components \n"
        "check_required_components(${ARGV_NAME}) \n"
            )

     # 生成 HqConfig.cmake
     configure_package_config_file(
         "${IN_CONFIG_PATH}"
         "${CMAKE_CURRENT_BINARY_DIR}/cmake/${ARGV_NAME}Config.cmake"
         INSTALL_DESTINATION lib/cmake/${TARGET_NAME}
         PATH_VARS CMAKE_CURRENT_BINARY_DIR
     )
    # 安装配置文件
    install(FILES
       "${CMAKE_CURRENT_BINARY_DIR}/cmake/${ARGV_NAME}Config.cmake"
        DESTINATION lib/cmake/${ARGV_NAME}
    )
    # 生成版本文件
    write_basic_package_version_file(
        "${CMAKE_CURRENT_BINARY_DIR}/cmake/${ARGV_NAME}ConfigVersion.cmake"
        VERSION ${ARGV_VERSION}
        COMPATIBILITY SameMajorVersion
    )
    # 安装版本文件
    install(FILES
        "${CMAKE_CURRENT_BINARY_DIR}/cmake/${ARGV_NAME}ConfigVersion.cmake"
        DESTINATION lib/cmake/${ARGV_NAME}
    )

endfunction()